﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Web.Profile;
using System.Web.UI.HtmlControls;
using System.Text;
using System.IO;
using Business;
using SubSonic;

public partial class User_Employer_AdvisorList : System.Web.UI.Page
{
    protected void Page_Load( object sender, EventArgs e )
    {
//        int i=0;
//        //GridView1.DataSource = ;
//        //GridView1.DataBind();
//        List<ProfileCommon> profileList = null;
// MembershipUserCollection allUsers = Membership.GetAllUsers();
//foreach (MembershipUser user in allUsers) 
//{
//    i+=1;
//ProfileCommon profile = Profile.GetProfile(user.UserName);
//profileList.Add(profile);
//}
//GridView1.DataSource = profileList;
//GridView1.DataBind();
        try
        {
         

            SubSonic.Query q = DataAccess.TblUserDetail.CreateQuery();
            q.QueryType = SubSonic.QueryType.Select;
            q.OR( DataAccess.TblUserDetail.Columns.Role, SubSonic.Comparison.Equals, "Advisorhoghooghi" );
            q.OR( DataAccess.TblUserDetail.Columns.Role, SubSonic.Comparison.Equals, "Advisorhaghighi" );
            GridViewAdvisors.DataSource = q.ExecuteDataSet();
            GridViewAdvisors.DataBind();

          
        }
        catch
        {
        }
    }
    public void setnull(int row, GridViewRowEventArgs e)
{
    e.Row.Cells[row].Text = "---";
}
    protected void GridViewAdvisors_RowDataBound( object sender, GridViewRowEventArgs e )
    {

        try
        {
            BindingGrid(e);
        }
        catch
        {
        }
    }

    private void BindingGrid(GridViewRowEventArgs e)
    {
        string orderExpression = DataAccess.TblUserDetail.Columns.Name;
        SubSonic.Query tempQuery = new Query(DataAccess.TblUserDetail.Schema);
        tempQuery.OrderBy = SubSonic.OrderBy.Asc("Name");
        if (ViewState["SortExpression"] != null && !ViewState["SortExpression"].ToString().Trim().Equals(String.Empty))
        {
            switch (ViewState["SortExpression"].ToString())
            {
                case "Name":
                    orderExpression = DataAccess.TblUserDetail.Columns.Name;
                    break;
                case "Family":
                    orderExpression = DataAccess.TblUserDetail.Columns.Family;
                    break;
                case "UserName":
                    orderExpression = DataAccess.TblUserDetail.Columns.UserName;
                    break;
                case "Province":
                    
                    break;
                case "SkillYears":
                    break;
                case "Degree":
                    break;
                case "Field2":
                    break;
                case "Field1":
                    break;
                case "ValidationDate":
                    break;
                case "CompanyRegistrationCode":
                    break;
                case "Role":
                    break;
            }

            tempQuery.OrderBy = ViewState["SortDirection"] != "Ascending" ? SubSonic.OrderBy.Desc(orderExpression) : SubSonic.OrderBy.Asc(orderExpression);
        }
        if (e.Row.RowType
            == DataControlRowType.DataRow)
        {
            Guid USerid = new Guid(e.Row.Cells[18].Text);

            ProfileCommon profile = Profile.GetProfile(Membership.GetUser(USerid).UserName);

            string p = e.Row.Cells[6].Text;
            if (p == "Advisorhoghooghi")
            {
                if (profile.HoghooghiAdvisor.CompanyRegistrationCode != "")
                    e.Row.Cells[8].Text = profile.HoghooghiAdvisor.CompanyRegistrationCode;
                else
                    setnull(8, e);
                if (profile.HoghooghiAdvisor.ValidationDate != "")
                {
                    string pdate =
                        new Business.Calender().MiladiToPersian(Convert.ToDateTime(profile.HoghooghiAdvisor.ValidationDate));
                    e.Row.Cells[9].Text = pdate;
                }
                else
                    setnull(9, e);
                if (profile.HoghooghiAdvisor.Branch != "")
                    e.Row.Cells[12].Text = profile.HoghooghiAdvisor.Branch;
                else
                    setnull(12, e);

                if (profile.HoghooghiAdvisor.ExtendedBranch != "")
                    e.Row.Cells[13].Text = profile.HoghooghiAdvisor.ExtendedBranch;
                else
                    setnull(13, e);

                e.Row.Cells[6].Text = "مشاور حقوقی";
            }
            if (p == "Advisorhaghighi")
            {
                if (profile.HaghighiAdvisor.Branch != "")
                    e.Row.Cells[12].Text = profile.HaghighiAdvisor.Branch;
                else
                    setnull(12, e);
                if (profile.HaghighiAdvisor.GraduateGrade != "")
                    e.Row.Cells[14].Text = profile.HaghighiAdvisor.GraduateGrade;
                else
                    setnull(14, e);
                if (profile.HaghighiAdvisor.GraduateDate != "")
                {
                    string pdate =
                        new Business.Calender().MiladiToPersian(Convert.ToDateTime(profile.HaghighiAdvisor.GraduateDate));
                    e.Row.Cells[15].Text = pdate;
                }
                else
                    setnull(15, e);

                e.Row.Cells[6].Text = "مشاور حقیقی";
            }

            e.Row.Cells[16].Text = profile.Common.WorkYears;
            if (profile.Common.WorkYears == "")
                setnull(16, e);
            e.Row.Cells[17].Text = profile.Common.City;
            if (profile.Common.City == "")
                setnull(17, e);
            e.Row.Cells[10].Text = profile.Contacts.Email;
            if (profile.Contacts.Email == "")
                setnull(10, e);
            e.Row.Cells[11].Text = profile.Contacts.Site;
            if (profile.Contacts.Site == "")
                setnull(11, e);
            e.Row.Cells[18].Visible = false;
        }
    }

    public override void VerifyRenderingInServerForm(Control control)
    {
    }
    protected void ExportToExcel_Click(object sender, EventArgs e)
    {

        try
        {
            Response.Clear();
            Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
            Response.ContentType = "application/vnd.ms-excel";
            Response.ContentEncoding = System.Text.Encoding.Unicode;
            Response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble());
            this.EnableViewState = false;
            StringWriter sr = new StringWriter();
            HtmlTextWriter hr = new HtmlTextWriter(sr);
            GridViewAdvisors.RenderControl(hr);
            Response.Write(sr.ToString());
            sr.Dispose();
            hr.Dispose();
            Response.End();
        }
        catch (Exception ex)
        {
        }
    }
    protected void GridViewAdvisors_RowCreated(object sender, GridViewRowEventArgs e)
    {
        //if (e.Row.RowType == DataControlRowType.DataRow)
        //{



        //    e.Row.Attributes.Add("onMouseOver", "this.originalstyle=this.style.backgroundColor;this.originalstylesize=this.style.fontSize;this.style.backgroundColor='lightblue';this.style.fontSize='110%'");

        //    e.Row.Attributes.Add("onMouseOut", "this.style.backgroundColor=this.originalstyle;this.style.fontSize=this.originalstylesize");

        //}


    }

    protected void GridViewAdvisors_DataBound(object sender, EventArgs e)
    {
        if (GridViewAdvisors.Rows.Count > 0)
        {
            //This replaces <td> with <th> and adds the scope attribute
            GridViewAdvisors.UseAccessibleHeader = true;

            //This will add the <thead> and <tbody> elements
            GridViewAdvisors.HeaderRow.TableSection = TableRowSection.TableHeader;

            //This adds the <tfoot> element. 
            //Remove if you don't have a footer row
            GridViewAdvisors.FooterRow.TableSection = TableRowSection.TableFooter;
        }
    }
}